package com.zc.timedown.service.impl;

import com.zc.timedown.constants.AchievementTypeConstants;
import com.zc.timedown.domain.Achievement;
import com.zc.timedown.mapper.AchievementMapper;
import com.zc.timedown.mapper.TodayPlanMapper;
import com.zc.timedown.service.ApiAchievementService;
import com.zc.timedown.vo.AchievementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * ClassName:ApiAchievementServiceImpl
 * Description:
 * DateTime: 2025/10/22 14:08
 * Author: zc
 * Version: 1.0
 */
@Service
public class ApiAchievementServiceImpl implements ApiAchievementService {

    @Autowired
    private AchievementMapper achievementMapper;

    @Override
    public List<AchievementVo> getUserAchievements(Long userId) {
        List<Achievement> achievements = achievementMapper.selectAchievementsByUserId(userId);
        ArrayList<AchievementVo> achievementVos = new ArrayList<>();
       for (int i = 0; i < 6; i++){
           AchievementVo achievementVo = new AchievementVo();
           achievementVo.setEarned(false);
           if (i == 0){
               achievementVo.setAchievementType(AchievementTypeConstants.CONSECUTIVE_3_DAYS);
               achievementVo.setMedal("初心勋章");
           }else if (i == 1){
               achievementVo.setAchievementType(AchievementTypeConstants.CONSECUTIVE_7_DAYS);
               achievementVo.setMedal("坚持勋章");
           }else if (i == 2){
               achievementVo.setAchievementType(AchievementTypeConstants.CONSECUTIVE_14_DAYS);
               achievementVo.setMedal("毅力勋章");
           }else if (i == 3){
               achievementVo.setAchievementType(AchievementTypeConstants.CONSECUTIVE_30_DAYS);
               achievementVo.setMedal("恒心勋章");
           }else if (i == 4){
               achievementVo.setAchievementType(AchievementTypeConstants.CONSECUTIVE_100_DAYS);
               achievementVo.setMedal("传奇勋章");
           }else if (i == 5){
               achievementVo.setAchievementType(AchievementTypeConstants.CONSECUTIVE_365_DAYS);
               achievementVo.setMedal("年度勋章");
           }
           achievementVos.add(achievementVo);
       }

        // 遍历数据库查询到的成就，匹配并赋值
        for (Achievement achievement : achievements) {
            try {
                String achievementType = achievement.getAchievementType();
                // 在achievementVos中查找对应类型的成就
                for (int i = 0; i < achievementVos.size(); i++) {
                    if (achievementType != null && achievementType.equals(achievementVos.get(i).getAchievementType())) {
                        // 找到匹配的成就类型，复制数据
                        AchievementVo vo = achievementVos.get(i);
                        vo.setId(achievement.getId());
                        vo.setUserId(achievement.getUserId());
                        vo.setAchievedDate(achievement.getAchievedDate());
                        vo.setCreateTime(achievement.getCreateTime());
                        vo.setUpdateTime(achievement.getUpdateTime());
                        vo.setEarned(true);
                        break;
                    }
                }
            } catch (Exception e) {
                // 处理可能的异常，如空指针等
                continue;
            }
        }
        return achievementVos;
    }

    @Override
    public int insertAchievement(Achievement achievement) {
        // 先查询是否已存在相同用户和类型的成就
        Achievement existingAchievement = achievementMapper.selectAchievementByUserAndType(
                achievement.getUserId(),
                achievement.getAchievementType()
        );

        if (existingAchievement != null) {
            // 如果存在，则更新时间
            achievement.setId(existingAchievement.getId());
            achievement.setUpdateTime(LocalDateTime.now());
            return achievementMapper.updateAchievement(achievement);
        } else {
            // 如果不存在，则创建新成就
            achievement.setCreateTime(LocalDateTime.now());
            achievement.setUpdateTime(LocalDateTime.now());
            return achievementMapper.insertAchievement(achievement);
        }
    }


}
